package com.school.cloud.controller;

import com.school.cloud.entities.CommonResult;
import com.school.cloud.entities.Payment;
import com.school.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 功能描述：
 *
 * @Package: com.school.cloud.controller
 * @author: msi-pc
 * @date: 2022/5/8 12:50
 */
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("结果为 : {} ", result);
        if (result > 0) {
            return new CommonResult(200, "success serverPort :" + serverPort, result);
        } else {
            return new CommonResult<>(444, "fail", null);
        }
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("结果为 : {} ", payment);
        if (payment != null) {
            return new CommonResult(200, "success serverPort :" + serverPort, payment);
        } else {
            return new CommonResult<>(444, "无", null);
        }
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB() {
        return serverPort;
    }
}
